﻿using BCCommon;
using BCCommon.Enums.TMS;
using BCDto.Common.ErrorMessage;
using BCDto.TMS.Fence;
using BCService.TMS.Fence;
using BCTMSRESTful.Configs;
using BCTMSRESTful.Controllers.Management;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BCTMSRESTful.Controllers.Management.TMS.Fence
{

    /// <summary>
    /// 电子围栏接口
    /// </summary> 
    [ApiExplorerSettings(GroupName = "TMSAdmin")]
    [Route("api/v1/sim/[controller]")]
    public class FenceController : ManagementBaseController
    {
        #region var
        private IFenceService fenceService;
        #endregion


        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary> 
        public FenceController(IFenceService fenceService)
        {
            this.fenceService = fenceService;
        }
        #endregion

        #region POST 添加 
        /// <summary>
        /// 添加新电子围栏纪录 
        /// </summary>
        /// <param name="dto">电子围栏数据模型</param>
        /// <returns></returns>
        [Log("添加新电子围栏纪录")]
        [HttpPost]
        [ProducesResponseType(typeof(FenceDto), 200)]
        public IActionResult Post([FromBody] FenceRequestDto dto)
        {
            var a = fenceService.Add(dto, this.Admin);
            return Ok(a);
        }

        #endregion

        #region PUT 修改   
        /// <summary>
        /// 修改电子围栏纪录
        /// </summary>
        /// <param name="dto">电子围栏数据模型</param>
        /// <returns></returns>
        [Log("修改电子围栏纪录")]
        [HttpPut]
        [ProducesResponseType(typeof(FenceDto), 200)] 
        public IActionResult Put([FromBody] FencePutDto dto)
        {
            var a = Ok(fenceService.Update(dto, this.Admin));
            return a;
        }
        #endregion

        #region 删除  
        /// <summary>
        /// 删除电子围栏纪录
        /// </summary>
        /// <param name="fenceId">电子围栏ID</param>
        /// <returns></returns>
        [Log("删除电子围栏纪录")]
        [HttpDelete("{fence_id}")] 
        public IActionResult Delete([FromRoute(Name = "fence_id")] long fenceId)
        {
            fenceService.Delete(fenceId);
            return NoContent();
        }
        #endregion

        #region GetByID     
        /// <summary>
        /// 获取一条电子围栏纪录
        /// </summary>
        /// <param name="fenceId">电子围栏ID</param>
        /// <returns></returns>
        [HttpGet("{fence_id}")]
        [ProducesResponseType(typeof(FenceDto), 200)] 
        public IActionResult Get([FromRoute(Name = "fence_id")] long fenceId)
        {
            return (Ok(fenceService.GetById(fenceId)));

        }
        #endregion

        #region Query 
        /// <summary>
        /// 查询电子围栏
        /// </summary>
        /// <param name="workCompanyName">劳务公司</param>
        /// <param name="provinceCode">省编码</param>
        /// <param name="cityCode">市编码</param>
        /// <param name="countyCode">区县编码</param> 
        /// <param name="fenceName">围栏名称 </param>
        /// <param name="isEnable">启用</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<FenceDto>), 200)] 
        public IActionResult Query(
            [FromQuery(Name = "working_company_name")] string workCompanyName,
            [FromQuery(Name = "fence_name")] string fenceName,
            [FromQuery(Name = "province_code")] string provinceCode,
            [FromQuery(Name = "city_code")] string cityCode,
            [FromQuery(Name = "county_code")] string countyCode,
            [FromQuery(Name = "is_enable")] bool? isEnable,
            [FromQuery(Name = "index")] int? pageIndex = 1, 
            [FromQuery(Name = "size")] int? pageSize = 10)
        {
            var a = Ok(fenceService.GetList(Admin, workCompanyName, provinceCode, cityCode, countyCode, fenceName, isEnable,pageIndex.Value, pageSize.Value));
            return a;
        }
        #endregion

        #region Query 
        /// <summary>
        /// 查询电子围栏列表
        /// </summary> 
        /// <param name="workCompanyName">劳务公司</param>
        /// <param name="provinceCode">省编码</param>
        /// <param name="cityCode">市编码</param>
        /// <param name="countyCode">区县编码</param> 
        /// <param name="fenceName">围栏名称 </param>
        /// <param name="isEnable">启用</param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(List<FenceDto>), 200)]
        public IActionResult GetList( 
            [FromQuery(Name = "working_company_name")] string workCompanyName,
            [FromQuery(Name ="fence_name")]string fenceName,
            [FromQuery(Name = "province_code")] string provinceCode,
            [FromQuery(Name = "city_code")] string cityCode,
            [FromQuery(Name = "county_code")] string countyCode,
            [FromQuery(Name = "is_enable")] bool? isEnable)
        {
            var a = Ok(fenceService.GetOnlyList(Admin,workCompanyName,provinceCode,cityCode,countyCode,fenceName,isEnable));
            return a;
        }
        #endregion


        #region 获取包含目标点围栏列表 
        /// <summary>
        /// 获取包含目标点围栏列表
        /// </summary>
        /// <param name="lng">经度</param>
        /// <param name="lat">维度</param>
        /// <param name="provinceCode">省编码</param>
        /// <param name="cityCode">市编码</param>
        /// <param name="countyCode">区县编码</param>
        /// <param name="gpsMapType">地图类型(不用传)</param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(List<FenceDto>), 200)]
        public IActionResult GetIntoFenceList( 
            [FromQuery(Name = "lng")] double lng,
            [FromQuery(Name = "lat")] double lat,
            [FromQuery(Name = "province_code")] string provinceCode,
            [FromQuery(Name = "city_code")] string cityCode,
            [FromQuery(Name = "county_code")] string countyCode,
            [FromQuery(Name = "GpsMapType")] GpsMapType? gpsMapType = GpsMapType.GDMAP)
        {
            var a = fenceService.GetIntoFenceList(lng, lat, provinceCode, cityCode, countyCode, gpsMapType.Value);
            return Ok(a);
        }
        #endregion



        #region 目标点是否在围栏内
        /// <summary>
        /// 目标点是否在围栏内
        /// </summary>
        /// <param name="fenceId"></param>
        /// <param name="lng"></param>
        /// <param name="lat"></param>
        /// <param name="gpsMapType"></param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(bool), 200)]
        public IActionResult IntoFence(
            [FromQuery(Name = "fence_id")] long fenceId,
            [FromQuery(Name = "lng")] double lng, 
            [FromQuery(Name = "lat")] double lat,
            [FromQuery(Name = "GpsMapType")] GpsMapType? gpsMapType= GpsMapType.GDMAP)
        {
            var a = fenceService.IsIntoFence(fenceId,lng, lat, gpsMapType.Value);
            return Ok( a);
        }
        #endregion
    }
}
